define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'choi/nav/index' + location.search,
                    add_url: 'choi/nav/add',
                    edit_url: 'choi/nav/edit',
                    del_url: 'choi/nav/del',
                    multi_url: 'choi/nav/multi',
                    table: 'nav',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                escape: false,
                pk: 'id',
                sortName: 'weigh',
                pagination: false,
                commonSearch: false,
                search: false,
                showExport: false,
                showToggle: false,
                showColumns: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'pid', title: __('Pid')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {field: 'type', title: __('Type')},
                        {field: 'child', title: __('Child'), table: table, formatter: Table.api.formatter.toggle},
                        // {field: 'image', title: __('Image'), events: Table.api.events.image, formatter: Table.api.formatter.image},
                        // {field: 'url', title: __('Url'), formatter: Table.api.formatter.url},
                        {field: 'description', title: __('Description')},
                        {field: 'onlineswitch', title: __('Onlineswitch'), table: table, formatter: Table.api.formatter.toggle},
                        {field: 'weigh', title: __('Weigh')},
                        // {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: function (e, data) {

                                var url = '';
                                if (data.type != 'all') {
                                    url += `<a href="javascript:;" class="btn btn-xs btn-primary btn-create" data-id="`+data.id+`" data-type="`+data.type+`" >内容管理</a> `;
                                }
                                    url += `<a href="javascript:;" class="btn btn-xs btn-primary btn-create" title="拖动进行排序"><i class="fa fa-arrows"></i></a> ` +
                                        ' <a href="/administration.php/choi/nav/edit/ids/1" class="btn btn-xs btn-success btn-editone" title="编辑"><i class="fa fa-pencil"></i></a> ' +
                                        ' <a href="javascript:;" class="btn btn-xs btn-danger btn-delone"  title="删除" ><i class="fa fa-trash"></i></a>';
                                return url;
                             }}
                    ]
                ]
            });

            table.on('click', '.btn-create', function () {
                var id = $(this).data('id')
                var type = $(this).data('type')

                //单页面
                if (type == 'page') {
                    Fast.api.open('/administration.php/choi/nav_page/edit/ids/'+id, '内容管理', {area: ['100%', '100%']});
                }
                if (type == 'list') {
                    Fast.api.open('/administration.php/choi/nav_list?ids='+id, '内容管理', {area: ['100%', '100%']});
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});